package com.originskyseed.platform.user.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.SpreadPlayerLimit;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.ISpreadPlayerLimitService;
import com.originskyseed.platform.user.vo.request.SpreadPlayerLimitRequestVO;

/**
 * 主播玩家推广上限记录表Controller类（OSI_SPREAD_PLAYER_LIMIT）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/spreadPlayerLimit")
@BaseLocaleResource(localeFileName = "i18n_user")
public class SpreadPlayerLimitController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SpreadPlayerLimitController.class);

	@DubboReference
	private ISpreadPlayerLimitService spreadPlayerLimitService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/spreadPlayerLimitList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<SpreadPlayerLimit>> spreadPlayerLimitList(HttpServletRequest request) {
		BaseResponseBody<BasePage<SpreadPlayerLimit>> result = null;
		try {
			// 请求对象
			SpreadPlayerLimitRequestVO requestVO = this.getParameterBean(SpreadPlayerLimitRequestVO.class);

			// 分页查询
			BasePage<SpreadPlayerLimit> page = new BasePage<SpreadPlayerLimit>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.spreadPlayerLimitService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<SpreadPlayerLimit>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<SpreadPlayerLimit>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/spreadPlayerLimitSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<SpreadPlayerLimit> spreadPlayerLimitSave(HttpServletRequest request) {
		BaseResponseBody<SpreadPlayerLimit> result = null;
		try {
			// 请求对象
			SpreadPlayerLimit spreadPlayerLimit = this.getParameterBean(SpreadPlayerLimit.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (StringUtils.isNotBlank(spreadPlayerLimit.getId())) {
				this.spreadPlayerLimitService.updateByPrimaryKeySelective(spreadPlayerLimit);
			} else {
				// 校验是否已经存在
				if (spreadPlayerLimit.getObjId() == null && StringUtils.isEmpty(spreadPlayerLimit.getObjId())) { // 新增系统默认
					SpreadPlayerLimit isNUllByObjId = this.spreadPlayerLimitService.selectIsNUllByObjId("-1");
					if (isNUllByObjId != null) {
						return new BaseResponseBody<SpreadPlayerLimit>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.USER_DATA_IS_NOTNULL.getMsg()), null);
					}
					spreadPlayerLimit.setObjId("-1");
					spreadPlayerLimit.setObjName("-1");
				} else {
					SpreadPlayerLimit isNUllByObjId = this.spreadPlayerLimitService.selectIsNUllByObjId(spreadPlayerLimit.getObjId());
					if (isNUllByObjId != null) {
						return new BaseResponseBody<SpreadPlayerLimit>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.USER_DATA_IS_NOTNULL.getMsg()), null);
					}
				}
				spreadPlayerLimit.setCreator(userInfo.getName());
				spreadPlayerLimit.setCreatorId(userInfo.getId());
				spreadPlayerLimit.setState(1);
				this.spreadPlayerLimitService.insertSelective(spreadPlayerLimit);
			}
			result = new BaseResponseBody<SpreadPlayerLimit>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), spreadPlayerLimit);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<SpreadPlayerLimit>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/spreadPlayerLimitDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> spreadPlayerLimitDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.spreadPlayerLimitService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 批量修改状态
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/spreadPlayerLimitUpdateState.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids", "state" })
	public BaseResponseBody<Boolean> spreadPlayerLimitUpdateState(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			int state = this.getParameterInteger("state", null);
			this.spreadPlayerLimitService.updateStateByIds(Arrays.asList(ids), state);
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.UPDATE_STATE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.UPDATE_STATE_SUCCESS.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
